<template>
  <div class="app-container">

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <!--查询表单-->
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="addTitle" placeholder="输入需要添加的分类"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%;margin-left: 20%;margin-right: 20%">
      <!-- 表格 -->
      <el-table :data="list" border stripe style="padding: 30px 0; text-align: center">
        <el-table-column prop="id" label="序号" width="200" align="center"/>
        <el-table-column prop="title" label="标题" width="250" align="center"/>

        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="openDialog(scope.row.id)">编辑
            </el-button>

            <el-button type="danger" size="mini" icon="el-icon-error" @click="remove(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20]"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="fetchData"
      @size-change="changePageSize"/>

    <!-- 编辑弹窗-->
    <el-dialog
      title="修改分类"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">

      <el-form :inline="true">
        <el-form-item>
          分类<el-input v-model="subject.title" placeholder="输入需要分类"/>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import subjectApi from '@/api/subject'

  export default {
    // 页内数据
    data() {
      return {
        list: [],
        page: 1,
        limit: 5,
        total: 0,
        addTitle: "",
        subject: {},
        dialogVisible: false
      }
    },

    //初始化
    created() {
      this.fetchData()
    },

    methods: {
      //获取数据
      fetchData(page = 1) {
        this.page = page;
        subjectApi.getList(this.page, this.limit)
          .then(response => {
            this.list = response.data.rows;
            this.total = response.data.total
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 改变每页记录数
      changePageSize(size) {
        this.limit = size;
        this.fetchData()
      },

      // 添加分类
      add() {
        subjectApi.add(this.addTitle);
        this.fetchData()
      },

      // 删除记录
      remove(id) {
        // 询问是否删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return subjectApi.delete(id)
        }).then(response => {
          // 刷新页面
          this.fetchData();
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
        })
      },

      //打开对话框
      openDialog(id) {
        this.dialogVisible = true;
        subjectApi.getSubject(id)
          .then(response => {
            this.subject = response.data.subject
          })
          .catch(error => {
            console.log(error)
          })
      },

      //更新分类
      update() {
        subjectApi.update(this.subject)
          .then(response => {
            this.dialogVisible = false;
            this.fetchData();
            this.subject = {}
          })
          .catch(error => {
            console.log(error)
          })
      }
    }
  }
</script>
